package com.yuerlive.paperplane.data.source.local;

import android.content.Context;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.yuerlive.paperplane.data.ZhihuDailyNewsQuestion;
import com.yuerlive.paperplane.data.source.datasource.ZhihuDailyNewsDataSource;
import com.yuerlive.paperplane.database.AppDatabase;
import com.yuerlive.paperplane.database.DatabaseCreator;

import java.util.List;

/**
 * Created by Admin on 2017/10/12.
 */

public class ZhihuDailyNewsLocalDataSource implements ZhihuDailyNewsDataSource{
    @Nullable
    private AppDatabase mDb = null;
    @Nullable
    private static ZhihuDailyNewsLocalDataSource instance=null;
    private ZhihuDailyNewsLocalDataSource(@NonNull Context context){
        DatabaseCreator creator =DatabaseCreator.getInstance();
        if(!creator.isDatabaseCreated()){
            creator.createDb(context);
        }
    }
    public static ZhihuDailyNewsLocalDataSource getInstance(@NonNull Context context){
        if(instance ==null){
            instance =new ZhihuDailyNewsLocalDataSource(context);
        }
      return  instance;
    }
    public static void destroyInstance(){
        instance=null;
    }
    @Override
    public void getZhihuDailyNews(boolean forceUpdate, boolean clearCache, long date, @NonNull loadZhihuDailyNewsCallback callback) {
        if(mDb ==null){
            mDb=DatabaseCreator.getInstance().getDatabase();
        }
        if(mDb!=null){
            new AsyncTask<Void,Void,List<ZhihuDailyNewsQuestion>>(){
                @Override
                protected void onPostExecute(List<ZhihuDailyNewsQuestion> zhihuDailyNewsQuestions) {
                    super.onPostExecute(zhihuDailyNewsQuestions);
                    if(zhihuDailyNewsQuestions==null){
                        callback.onDataNotAvailable();
                    }else{
                        callback.onNewsLoad(zhihuDailyNewsQuestions);
                    }
                }

                @Override
                protected List<ZhihuDailyNewsQuestion> doInBackground(Void... params) {
                    return mDb.zhihuDailyNewsDao().queryAllByData(date);
                }
            }.execute();
        }
    }

    @Override
    public void getFavorites(@NonNull loadZhihuDailyNewsCallback callback) {

    }

    @Override
    public void getItem(int itemId, @NonNull GetNewsItemCallback callback) {

    }

    @Override
    public void favoriteItem(int itemId, boolean favorite) {

    }

    @Override
    public void saveAll(@NonNull List<ZhihuDailyNewsQuestion> list) {
      if(mDb ==null){
          mDb =DatabaseCreator.getInstance().getDatabase();
      }
      if(mDb!=null){
          new Thread(()->{
              mDb.beginTransaction();
              try {
                  mDb.zhihuDailyNewsDao().insertAll(list);
                  mDb.setTransactionSuccessful();
              }finally {
               mDb.endTransaction();
              }
          }).start();
      }
    }
}
